//
//  HLPushAttachment.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLPushAttachment.h"

@implementation HLPushAttachment

- (nonnull NSString *)encodeAttachment {
    NSDictionary *dict = @{
                           @"msgType" : @(5),
                           @"ext" : @{
                               @"pushTitle" : self.pushTitle?:@"",
                               @"pushContent" : self.pushContent?:@"",
                               @"messageTitle" : self.messageTitle?:@"",
                               @"messageContent" : self.messageContent?:@"",
                               @"messageImageUrl" : self.messageImageUrl?:@"",
                                   @"imageWidth" : @(self.imageWidth),
                                   @"imageHeight" : @(self.imageHeight),
                                    }
                           };
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0
                                                     error:nil];
    NSString *content = nil;
    if (data) {
        content = [[NSString alloc] initWithData:data
                                        encoding:NSUTF8StringEncoding];
    }
    return content;
}

- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width
{
    NIMCustomObject *object = (NIMCustomObject *)message.messageObject;
    HLPushAttachment *attachment = (HLPushAttachment *)object.attachment;
    
    CGFloat height = 16; // 内容距离顶部的距离，固定的
    width = SCREEN_WIDTH - 32;
    
    if (attachment.messageTitle) {
        height += 19;
    }
    
    if (attachment.messageContent) {
        CGFloat msgHeight = [attachment.messageContent boundingRectWithSize:CGSizeMake(width - (16 * 2), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
        if (attachment.messageTitle) {
            height += 25 + msgHeight; // 上边有标题
        } else {
            height += msgHeight;// 上边没有标题，直接从顶部开始
        }
    }
    
    if (attachment.messageImageUrl.length) {
        if (height < 56) {
            height= 72;
        }
        
    }
    
    height += 16; // 内容距离底部的距离，也是固定的
    
    return CGSizeMake(width, height);
}

- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLPushContentView";
}

- (UIEdgeInsets)cellInsets:(NIMMessage *)message
{
    return UIEdgeInsetsMake(8, 16, 8, 16);
}

- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
    return UIEdgeInsetsZero;
}

- (BOOL)shouldShowAvatar:(NIMMessage *)message
{
    return NO;
}

@end
